package com.hdoit.ares.logical.acc.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.ares.logical.acc.dao.IAccountDetailDao;
import com.hdoit.ares.logical.acc.model.AccountDetail;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.viewmodel.acc.AccountDetailSearch;
import com.hdoit.ares.logical.viewmodel.acc.FindAccountDetail;
import com.hdoit.ares.selfcommon.enums.acc.MoneyType;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      caochengmeng
 * @email       
 * @createtime  2017年11月27日 09:19:20
 * @comment
 */
@Component
public class AccountDetailDaoImpl extends SuperDao<AccountDetail> implements IAccountDetailDao{

	@Override
	public List<AccountDetail> queryBalanceList(String custId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("accountDetail.*")
						.from(AccountDetail.class,"accountDetail")
						.andWhere("accountDetail.customerId=#{custId}")
						.setParam("custId", custId)
						.addOrder("accountDetail.time", OrderEnum.desc);
	    return queryList(finderExpression);
	}
	
	@Override
	public AccountDetail querySumIncomeMoney(FindAccountDetail findAccountDetail,String id){
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("sum(money) as incomeMoney").
		from(AccountDetail.class,"accountDetail")
						  .andEq("customerId", id)
						  .andEq("state","income");
		
		appendFinder(findAccountDetail,finderExpression);
		
		return queryModel(finderExpression);
	}

	@Override
	public AccountDetail querySumExpendMoney(FindAccountDetail findAccountDetail,String id) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("sum(money) as expendMoney").
		from(AccountDetail.class,"accountDetail")
						  .andEq("customerId", id)
						  .andEq("state","pay");
		
		appendFinder(findAccountDetail,finderExpression);
		
		return queryModel(finderExpression);
	}

	//后台按条件查询
	@Override
	public List<AccountDetail> queryList(AccountDetailSearch accountDetailSearch,PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("DISTINCT accountDetail.*","customer.userIdentity as userIdentity")
						.from(AccountDetail.class,"accountDetail")
						.leftJoin(Customer.class,"customer")
						.on("accountDetail.customerId", "customer.id")
						.setMainTableAlias("accountDetail");
		
		appendHoutai(accountDetailSearch,finderExpression);
		
	    return queryList(finderExpression,accountDetailSearch,pagerModel);
	}

	//前台的按条件查询
	@Override
	public List<AccountDetail> queryAccountDetail(FindAccountDetail findAccountDetail, String custId) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(AccountDetail.class)
						.andEq("customerId", custId);
		
		appendFinder(findAccountDetail,finderExpression);

		return queryList(finderExpression);
	}

	//前台的高级搜索条件
	private void appendFinder(FindAccountDetail findAccountDetail,IFinderExpression finderExpression){
		if(!StringUtils.isEmpty(findAccountDetail.getMoneyType())){
			finderExpression.andEq("moneyType", findAccountDetail.getMoneyType());
		}
		
		if(!StringUtils.isEmpty(findAccountDetail.getOrder())){
			if("desc".equals(findAccountDetail.getUpOrDown())){
				finderExpression.addOrder(findAccountDetail.getOrder(), OrderEnum.desc);
			}
			if("asc".equals(findAccountDetail.getUpOrDown())){
				finderExpression.addOrder(findAccountDetail.getOrder(), OrderEnum.asc);
			}
		}
		if(findAccountDetail.getStartDate()!=null){
			finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},time)>=0")
			  .setParam("startDate", findAccountDetail.getStartDate());
		}
		if(findAccountDetail.getEndDate()!=null){
			finderExpression.andWhere("TIMESTAMPDIFF(SECOND,#{endDate},time)<=0")
			  .setParam("endDate", findAccountDetail.getEndDate());
		}
	}
	
	@Override
	public AccountDetail queryAccount(String id) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("*")
		                .from(AccountDetail.class)
		                .andEq("id", id);
		
		return queryModel(finderExpression);
	}

	@Override
	public AccountDetail queryAccountDetailByTime(String customerId, Date startDate, Date endDate) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		finderExpression.select("COUNT(`id`) AS totalNum","SUM(`money`) AS totalMoney")
		                .from(AccountDetail.class)
		                .andWhere("customerId = #{customerId}")
		                .setParam("customerId", customerId)
		                .andWhere("TIMESTAMPDIFF(SECOND,#{startDate},time)>=0")
		                .setParam("startDate", startDate)
		                .andWhere("TIMESTAMPDIFF(SECOND,#{endDate},time)<=0")
		                .setParam("endDate", endDate)
		                .andWhere("moneyType = #{moneyType}")
		                .setParam("moneyType", MoneyType.applyfor);
		
		return queryModel(finderExpression);
	}
	
	//后台的高级搜索条件
	private void appendHoutai(AccountDetailSearch search, IFinderExpression finder) {
		
		if(search.getStartDate()!=null){
			finder.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},accountDetail.time)>=0")
				  .setParam("startDate", search.getStartDate());
		}
		if(search.getEndDate()!=null){
			finder.andWhere("TIMESTAMPDIFF(SECOND,#{endDate},accountDetail.time)<=0")
				  .setParam("endDate", search.getEndDate());
		}
		
		if(!StringUtils.isEmpty(search.getUserIdentity())){
			finder.andWhere("customer.userIdentity like #{userIdentity}")
			  	  .setParam("userIdentity","%"+search.getUserIdentity()+"%");
		}
		if(!StringUtils.isEmpty(search.getAccountNo())){
			finder.andWhere("accountDetail.accountNo like #{accountNo}")
			  	  .setParam("accountNo","%"+search.getAccountNo()+"%");
		}		
		if(!StringUtils.isEmpty(search.getServiceId())){
			finder.andWhere("accountDetail.serviceId like #{serviceId}")
				  .setParam("serviceId","%"+search.getServiceId()+"%");
		}
		if(!StringUtils.isEmpty(search.getMoneyType())){
			finder.andWhere("accountDetail.moneyType = #{moneyType}")
				  .setParam("moneyType", search.getMoneyType());
		}		
	}

	@Override
	public int queryCount(AccountDetailSearch accountDetailSearch) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("count(DISTINCT accountDetail.id) as count")
						.from(AccountDetail.class,"accountDetail")
						.leftJoin(Customer.class,"customer")
						.on("accountDetail.customerId", "customer.id")
						.setMainTableAlias("accountDetail");
		
		appendHoutai(accountDetailSearch,finderExpression);
		
		return queryCount(finderExpression, accountDetailSearch);
	}

	@Override
	public AccountDetail queryModelByServiceId(String serviceId) {
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		
		finderExpression.select("*")
		                .from(AccountDetail.class)
		                .andEq("serviceId", serviceId);
		
		return queryModel(finderExpression);
	}

	//查询当前页面的账户余额列表
	@Override
	public List<AccountDetail> queryAccountList(
			FindAccountDetail findAccountDetail, String custId, Integer start,Integer length) {
		
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("*")
						.from(AccountDetail.class)
						.andEq("customerId", custId)
						.limit(start, length);
		
		appendFinder(findAccountDetail,finderExpression);

		return queryList(finderExpression);
	}

	/*//查询当前页面的总收入
	@Override
	public AccountDetail querySumIncome(FindAccountDetail findAccountDetail,
			String id, Integer start, Integer length) {
		
		IFinderExpression oneChildIdFinder=FinderExpressionFactory.createExpression();
		oneChildIdFinder.select("*")
						.from(AccountDetail.class)
						.andWhere("customerId=#{id}")
						.limit(start, length);
		
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("sum(money) as incomeMoney")
		                .from("("+oneChildIdFinder.toSql()+") as nowTable")
		                .andEq("state","income")
		                .setParam("id", id);
		
		appendFinder(findAccountDetail,finderExpression);
		
		return queryModel(finderExpression);
	}

	//查询当前页面的总支出
	@Override
	public AccountDetail querySumExpend(FindAccountDetail findAccountDetail,
			String id, Integer start, Integer length) {
		
		IFinderExpression oneChildIdFinder=FinderExpressionFactory.createExpression();
		oneChildIdFinder.select("*")
						.from(AccountDetail.class)
						.andWhere("customerId=#{id}")
						.limit(start, length);
		
		IFinderExpression finderExpression = FinderExpressionFactory.createExpression();
		finderExpression.select("sum(money) as expendMoney")
		                .from("("+oneChildIdFinder.toSql()+") as nowTable")
		                .andEq("state","pay")
		                .setParam("id", id);
		
		appendFinder(findAccountDetail,finderExpression);
		
		return queryModel(finderExpression);
	}*/
	
}